median = .50 # Used as goal reputations
reputation_adjust = 0 # Used to slightly tweak the median as needed to get above average hunt count

# Used to figure out if the average program is getting hunted against more than I am
average_reputation_per_round = [0]
average_hunts_per_player = [0]
hunts_against_me = [0]

total_players = 0
round_num = 0 # Used to track the round number because the goal reputation calculations are all made within the first 100 rounds before anyone is kicked out

def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):
    """The main routine that plays each individual round."""
    

    # define global variables
    global median
    global reputation_adjust

    global average_reputation_per_round
    global average_hunts_per_player
    
    global total_players
    global round
    round_num = round_number


    if round_num == 1:
        total_players = len(player_reputations)

    # Set average_reputation_per_round, average_hunts_per_player, total_choices
    average_rep = 0
    for x in player_reputations:
        average_rep += x
    average_rep /= len(player_reputations)
    average_reputation_per_round.append(average_rep)
    average_hunts_per_player.append(len(player_reputations) * average_reputation_per_round[-1])
	
    # Set ordered_reputations in order from player_reputations list
    ordered_reputations = player_reputations
    for y in range(0,len(ordered_reputations)):    
        for x in range(0,(len(ordered_reputations) - 1)):
            if (ordered_reputations[x] > ordered_reputations[x+1]):
                temp = ordered_reputations[x]
                ordered_reputations[x] = ordered_reputations[x+1]   
                ordered_reputations[x+1] = temp

    # Set ordered_reputations_wo_outliers for good approximation
    lowcap = 0
    highcap = len(ordered_reputations)-1
    for x in range(0, (len(ordered_reputations)-1)):
        if (ordered_reputations[x] == 0):
            lowcap = x + 1
        elif (ordered_reputations[x] == 1):
            highcap = x
    ordered_reputations_wo_outliers = ordered_reputations[lowcap:highcap]

    if(round_num < 100): # Nobody can be out before round 99 so do all the calculations that require all the players still
        # Set median at 50th percentile
        half = round(len(ordered_reputations_wo_outliers)*.5,0)
        median = ordered_reputations_wo_outliers[int(half)]

    # Set goal_rep and use that to determine how many times you need to hunt this round
    goal_reputation = median + reputation_adjust
    hunts_this_round = len(player_reputations)
    hunts_this_round *= goal_reputation
    hunts_this_round -= 1
    hunts_this_round = int(hunts_this_round)

    # Set the range of reputations you will commit to hunting in
    should_stop = 0
    hunt_rep_range=[0,0]
    for x in range(0,len(player_reputations)-1):
        if (ordered_reputations[x] > goal_reputation and should_stop == 0):
            hunt_rep_range[0] = ordered_reputations[int(round(x-(hunts_this_round/2),0))]
            hunt_rep_range[1] = ordered_reputations[int(round(x+(hunts_this_round/2),0))-1]
            should_stop = 1
    if hunt_rep_range[1] == 1:
        hunt_rep_range[1] = .98

    # Make final decisions
    hunt_decisions = ['s']
    if ((len(player_reputations)/total_players) < .1):
        hunt_decisions.pop()
        for x in player_reputations:
            hunt_decisions.append('s')
    else: 
        hunt_decisions.pop()
        for x in player_reputations:
            if x > hunt_rep_range[0] and x < hunt_rep_range[1]:
                hunt_decisions.append('h')
            else:
                hunt_decisions.append('s')
    return hunt_decisions

def hunt_outcomes(food_earnings):

    global reputation_adjust
    global hunts_against_me 
    global average_hunts_per_player

    # Adjust my goal reputation depending on how often people hunt against me compared to the average    
    if round_num < 100:
        hunt_count=0
        for x in food_earnings:
            if x == 0 or x == -3:
                hunt_count += 1
        hunts_against_me.append(hunt_count)
        if hunts_against_me[-1] > average_hunts_per_player[-1]:
            reputation_adjust -= .01
        elif hunts_against_me[-1] < average_hunts_per_player[-1]:
            reputation_adjust += .01

   

def round_end(award, m, number_hunters):
    pass



